package com.yeshine.opendata.service.impl;

import com.yeshine.opendata.core.ServiceBase;
import com.yeshine.opendata.dao.IAppThemeDao;
import com.yeshine.opendata.entity.AppTheme;
import com.yeshine.opendata.service.IAppThemeService;
import com.yeshine.opendata.utils.FileUtils;
import com.yeshine.opendata.utils.PageResult;
import com.yeshine.opendata.utils.Result;
import com.yeshine.opendata.utils.UploadConstantPath;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * Created by taohj on 2014/8/1.
 */
@Service
public class AppThemeServiceImpl extends ServiceBase<AppTheme> implements IAppThemeService {

    private IAppThemeDao dao;

     private  String path= UploadConstantPath.APPTHEME_FILE_PATH;

    @Autowired
    public void setDao(IAppThemeDao dao) {
        this.dao = dao;
    }




    @Override
    public Result<AppTheme> create(AppTheme theme) {
        return new Result<AppTheme>(dao.insert(theme) > 0);
    }

    @Override
    public Result<AppTheme> remove(AppTheme theme) {
        return new Result<AppTheme>(dao.delete(theme) > 0);
    }

    @Override
    public Result<AppTheme> modify(AppTheme theme) {
        return new Result<AppTheme>(dao.update(theme) > 0);
    }


    @Override
    public Result<AppTheme> getById(Integer id) {
        return new Result<AppTheme>(dao.selectById(id));
    }

    @Override
    public Result<AppTheme> getList(AppTheme theme) {

        return new Result<AppTheme>(dao.selectList(theme));
    }

    @Override
    public Result<AppTheme> getByPage(PageResult<AppTheme> pageResult) {
        pageResult.setRows(dao.selectbyPage(pageResult));
        return new Result<AppTheme>(pageResult);

    }

    @Override
    public Result<AppTheme> modify(AppTheme theme, HttpServletRequest request) {
        String path = this.path;
        MultipartFile image=theme.getImageFile();
        if (image!=null && image.getSize()>0){
            theme.setImage(path+theme.getId()+"_"+image.getOriginalFilename());
            try {
                FileUtils.copy(image.getInputStream(), new File(request.getSession().getServletContext().getRealPath("/"), theme.getImage()));
            } catch (IOException e) {
                e.printStackTrace();
                return new Result<AppTheme>(false, e.getMessage());
            }
        }else {
            theme.setImage("resources/images/app/default.jpg");
        }
        return this.modify(theme);
    }

    @Override
    public Result<AppTheme> create(AppTheme theme, HttpServletRequest request) {
        String path = this.path;
        Result<AppTheme> result=this.create(theme,request);
        if (!result.isSuccess()){
            return result;
        }

        MultipartFile image=theme.getImageFile();
        if (image!=null && image.getSize()>0){
            theme.setImage(path+theme.getId()+"_"+image.getOriginalFilename());
            try {
                FileUtils.copy(image.getInputStream(), new File(request.getSession().getServletContext().getRealPath("/"), theme.getImage()));
            } catch (IOException e) {
                e.printStackTrace();
                return new Result<AppTheme>(false, e.getMessage());
            }
        }else {
            theme.setImage("resources/images/app/default.jpg");
        }
        return this.modify(theme);
    }
}
